package ntd.util.frame.task.handle.msg;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.log.StaticLog;
import ntd.task.Task;
import ntd.task.bo.TaskInfo;
import ntd.util.config.common.enums.EnumPlatformType;
import ntd.util.config.common.enums.msg.EnumMsgHandle;
import ntd.util.config.common.enums.msg.EnumMsgType;
import ntd.util.frame.msg.MsgHandle;
import ntd.util.frame.msg.service.context.MsgContext;
import ntd.util.modular.function.entity.ChannelBusiness;
import ntd.util.modular.function.service.ChannelBusinessService;
import ntd.util.modular.msg.entity.SmsTemplate;
import ntd.util.modular.msg.service.SmsTemplateService;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

@Component
public class SmsSyncTask implements Task {

    private static ChannelBusinessService channelBusinessService;
    private static SmsTemplateService smsTemplateService;

    @PostConstruct
    public void init() {
        channelBusinessService = SpringUtil.getBean(ChannelBusinessService.class);
        smsTemplateService = SpringUtil.getBean(SmsTemplateService.class);
    }

    public Boolean handle() {
        StaticLog.info("开始执行短信模板同步功能......");
        // 查询所有渠道商
        List<ChannelBusiness> channelBusinesses = channelBusinessService.list();
        // 查询短信通道
        List<EnumMsgHandle> enumMsgHandles = EnumMsgHandle.listByMsgType(EnumMsgType.SMS);
        // 短信模板
        List<SmsTemplate> smsTemplates = smsTemplateService.list();
        // 调用各个模块短信模板功能拿到短信模板列表
        for (EnumMsgHandle enumMsgHandle : enumMsgHandles) {
            for (ChannelBusiness channelBusiness : filterChannelBusiness(enumMsgHandle.getEnumPlatformType(), channelBusinesses)) {
                MsgHandle msgHandle = enumMsgHandle.getMsgHandle();
                MsgContext msgContext = MsgContext.init(channelBusiness);
                msgContext.setAction("smsSync");
                msgContext.setSmsTemplates(filterSmsTemp(smsTemplates, channelBusiness.getId()));
                msgHandle.handle(msgContext);
            }
        }
        // 同步到数据库
        smsTemplateService.saveBatch(smsTemplates);
        StaticLog.info("执行短信模板同步功能完成......");
        return Boolean.TRUE;
    }

    private List<ChannelBusiness> filterChannelBusiness(EnumPlatformType enumPlatformType, List<ChannelBusiness> channelBusinesses) {
        return CollUtil.filter(channelBusinesses, channelBusiness -> enumPlatformType.equals(channelBusiness.getEnumPlatformType()));
    }

    private List<SmsTemplate> filterSmsTemp(List<SmsTemplate> smsTemplates, String channelBusinessId) {
        return CollUtil.filter(smsTemplates, smsTemplate -> smsTemplate.getChannelBusinessId().equals(channelBusinessId));
    }

    @Override
    public TaskInfo info() {
        return new TaskInfo("任务-短信模板同步", "task-sms-sync", new SmsSyncTask());
    }

    @Override
    public void execute(Object o) {
        handle();
    }
}
